package image;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageManager {
	
	/**
	 * 图片路径
	 */
	private static String path = System.getProperty("user.dir") + "/res/";
	
	/**
	 * 鼠标图片
	 */
	public static BufferedImage mouseCentor;
	
	public static BufferedImage firebat;
	
	public static BufferedImage marine[];
	
	static {
		readAllImage();
	}
	
	/**
	 * 初始化所有图片
	 */
	public static void readAllImage(){
		
		mouseCentor = readFile("cur.png");
		firebat = readFile("0_firebat_atk0.gif");
		marine = getAllStateImage(getChildImage("0_marine_red.gif",3,5));
	}
	
	/**
	 * 传入一张原始图片,按传入的参数将其横竖切片成若干个子图片
	 * @param imageName 图片名字
	 * @param partRow 横着分成几等份
	 * @param partCol 竖着分成几等份
	 * @return 返回一个BufferedImage子图片数组
	 */
	public static BufferedImage[] getChildImage(String imageName, int partRow, int partCol){
		BufferedImage oldImage = readFile(imageName);
		int width = oldImage.getWidth()/partRow;//子图片的宽度
		int height = oldImage.getHeight()/partCol;//子图片的宽度
		BufferedImage[] images = new BufferedImage[partRow * partCol];
		for (int i = 0; i < partCol; i++) { // 5
			for (int j = 0; j < partRow; j++) { // 3
				images[j+(i*partRow)] = oldImage.getSubimage(j * width, i * height, width, height);
			}
		}
		return images;
	}
	
	/**
	 * 根据转入的图片,进行处理获得一个元素的所有方向以及移动的图片数组(只针对3*5)
	 * @param images 
	 * @return
	 */
	public static BufferedImage[] getAllStateImage(BufferedImage[] oldimages){
		BufferedImage[] images = new BufferedImage[24];
		for (int i = 0; i < oldimages.length; i++) {
			images[i] = oldimages[i];
		}
		
		for (int i = 0; i < 3; i++) {
			images[15 + i] = imageVerticalTransform(oldimages[9 + i]);
		}
		for (int i = 0; i < 3; i++) {
			images[18 + i] = imageVerticalTransform(oldimages[6 + i]);
		}
		for (int i = 0; i < 3; i++) {
			images[21 + i] = imageVerticalTransform(oldimages[3 + i]);
		}
		
		return images;
	}
	
	/**
	 * 图片垂直旋转(里面隐藏垂直和旋转n度方法)
	 * @param image 指定BufferedImage图片
	 * @return 旋转后的图片
	 */
	public static BufferedImage imageVerticalTransform(BufferedImage image){
        BufferedImage transformImage = null;     
        AffineTransform transform = new   AffineTransform(-1,0,0,1,image.getWidth()-1,0);//水平翻转     
//      AffineTransform transform = new   AffineTransform(1,0,0,-1,0,sourceImage.getHeight()-1);//垂直翻转     
//      AffineTransform transform = new   AffineTransform(-1,0,0,-1,sourceImage.getWidth()-1,sourceImage.getHeight()-1);//旋转180度     
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);     
        transformImage = op.filter(image, null); 
		return transformImage;
	}
	
	
	/**
	 * 读取图片
	 * @param imageName
	 * @return
	 */
	private static BufferedImage readFile(String imageName){
		try {
			return ImageIO.read( new File(path + imageName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("图片 " + imageName + " 读取失败!");
		return null;
	}
}
